<?php
function generatePagination($currentPage, $totalPage) {
    // 获取当前的 URL 查询参数
    $queryString = $_SERVER['QUERY_STRING'];
    parse_str($queryString, $queryParams);


    // 显示前三页
    for ($i = 1; $i <= min(3, $totalPage); $i++) {
        $queryParams['page'] = $i; // 设置当前页

        $activeClass = ($i == $currentPage) ? 'active' : ''; // 判断是否为当前页
        $output .= '<li class="page-item ' . $activeClass . '">
                        <a class="page-link h-44 w-44 flex-center text-15 rounded-8 fw-medium" href="?' . http_build_query($queryParams) . '">' . $i . '</a>
                    </li>';
    }

    // 添加省略号
    if ($currentPage > 4) {
        $output .= '<li class="page-item">
                        <a class="page-link h-44 w-44 flex-center text-15 rounded-8 fw-medium" href="#">...</a>
                    </li>';
    }

    // 显示当前页前后的页面
    $startPage = max(4, $currentPage - 3);
    $endPage = min($totalPage - 3, $currentPage + 3);

    for ($i = $startPage; $i <= $endPage; $i++) {
        $queryParams['page'] = $i; // 设置当前页

        $activeClass = ($i == $currentPage) ? 'active' : ''; // 判断是否为当前页
        $output .= '<li class="page-item ' . $activeClass . '">
                        <a class="page-link h-44 w-44 flex-center text-15 rounded-8 fw-medium" href="?' . http_build_query($queryParams) . '">' . $i . '</a>
                    </li>';
    }

    // 添加省略号
    if ($endPage < $totalPage - 3) {
        $output .= '<li class="page-item">
                        <a class="page-link h-44 w-44 flex-center text-15 rounded-8 fw-medium" href="#">...</a>
                    </li>';
    }

    // 显示最后三页
    for ($i = max($totalPage - 2, $endPage + 1); $i <= $totalPage; $i++) {
        $queryParams['page'] = $i; // 设置当前页
        if ($i <= 3) {
            continue;
        }
        $activeClass = ($i == $currentPage) ? 'active' : ''; // 判断是否为当前页
        $output .= '<li class="page-item ' . $activeClass . '">
                        <a class="page-link h-44 w-44 flex-center text-15 rounded-8 fw-medium" href="?' . http_build_query($queryParams) . '">' . $i . '</a>
                    </li>';

    }

    return $output;
}
/*$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1; // 获取当前页面
$totalPage = 10; // 总页数*/

echo generatePagination($nowPage, $totalPage);
?>
